using System;
using System.Collections.Generic;
using System.Text;
using Rapier.Kodachi.Shared;
using System.IO;
using System.Drawing;

namespace Rapier.Kodachi.Core.Actions
{
    public class AppendTo : Action
    {
        #region Constructor
        public AppendTo(Plugin plugin) 
            : base(plugin, "Rapier.CorePlugin.AppendTo", "Append to...", "Appends Direct Item to Indirect Item (Text File).")
        {
            //CoreCatalog.Cre
            IndirectItemUse = IndirectItemUse.Required;
            string itemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
            
            //CoreCatalog.CreateImageFromPath(this, itemPath);
            GetImage = delegate() { return CoreCatalog.GetImageFromPath(itemPath); };
        }
        #endregion

        #region Action Members
        public override bool IsValidDirect(Item directItem)
        {
            return directItem.Properties.ContainsKey("Text");
        }

        public override bool IsValidIndirect(Item indirectItem)
        {
            return indirectItem.Properties.ContainsKey("Path") && CorePlugin.IsTextFile(indirectItem);
        }

        public override void Execute(Item[] directItems, Item[] indirectItems)
        {
            foreach (Item directItem in directItems)
            {
                foreach (Item indirectItem in indirectItems)
                {
                    using (StreamWriter writer = new StreamWriter(indirectItem.Properties["Path"].ToString(), true))
                    {
                        writer.WriteLine(directItem.Properties["Text"]);
                    }
                }
            }
        }
        #endregion
    }
}
